/**
 * 
 */
package site.service;

import java.sql.Connection;
import java.util.LinkedHashMap;
import java.util.List;

import neoutil.CommonService;
import neoutil.DaoSupport;
import site.dao.CategoryDao;
import site.entity.Category;

/**
 * @author Neo
 * 
 */
public class CategoryRepository extends CommonService {

	public void saveCategory(Category category) {
		Connection connection = null;
		try {
			connection = DaoSupport.getConnection();

			CategoryDao categoryDao = new CategoryDao(connection);

			categoryDao.save(category);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeConnection(connection);
		}
	}

	public List<Category> getCategoriesByParentCid(int pcid) {
		Connection connection = null;
		try {
			connection = DaoSupport.getConnection();

			CategoryDao categoryDao = new CategoryDao(connection);

			String sql = "SELECT * FROM category WHERE parentCid = ? AND cid != ?";

			return categoryDao.find(sql, new Object[] { pcid, pcid });
		} catch (Exception e) {
			e.printStackTrace();

			return null;
		} finally {
			closeConnection(connection);
		}
	}

	public List<Category> getCategories(int cid) {
		Connection connection = null;
		try {
			connection = DaoSupport.getConnection();

			CategoryDao categoryDao = new CategoryDao(connection);

			String sql = "SELECT * FROM category WHERE parentCid=(SELECT parentCid FROM category WHERE id=?)";

			return categoryDao.find(sql, new Object[] { cid });
		} catch (Exception e) {
			e.printStackTrace();

			return null;
		} finally {
			closeConnection(connection);
		}
	}

	private List<Category> getCategoriesByLevel(int level)
	{
		if(level > 2) return null;
		Connection connection = null;
		try {
			connection = DaoSupport.getConnection();

			CategoryDao categoryDao = new CategoryDao(connection);

			String sql = "SELECT * FROM category WHERE level = " + level;

			return categoryDao.find(sql);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			closeConnection(connection);
		}
	}
	
	public List<Category> getAllSubCategories() {
		return getCategoriesByLevel(2);
	}
	
	public List<Category> getAllParentCategories() {
		return getCategoriesByLevel(1);
	}

	public Category getCategoryByCid(int cid) {
		Connection connection = null;
		try {
			connection = DaoSupport.getConnection();

			CategoryDao categoryDao = new CategoryDao(connection);

			String sql = "SELECT * FROM category WHERE cid=?";

			return categoryDao.find(sql, new Object[] { cid }).get(0);
		} catch (Exception e) {
			e.printStackTrace();

			return null;
		} finally {
			closeConnection(connection);
		}
	}

	public LinkedHashMap<Category, List<Category>> getAllCategoryTree() {
		LinkedHashMap<Category, List<Category>> map = new LinkedHashMap<Category, List<Category>>();
		
		List<Category> parents = getAllParentCategories();
		for(Category c : parents)
		{
			List<Category> cats = getCategoriesByParentCid(c.getCid());			
			map.put(c, cats);
		}
		
		return map;
	}
	
	public static void main(String[] args) {
		CategoryRepository cr = new CategoryRepository();
		cr.getAllCategoryTree();
	}

}
